<?php
// 内容控制器
class ContentController extends Controller{
    //主页
    public function index(){
      	// 1,商铺
      	$sid=Q('get.sid',0,'intval');
      	$sto_data=K('Store')->get_data(array('sid'=>$sid));
      	$sto_data=current($sto_data);
        $this->assign('sto_data',$sto_data);

      	// 2,菜类及菜品信息
      	$type_data=K('Type')->get_data(array('bd_store_sid'=>$sid));
      	$this->assign('type_data',$type_data);
     	  $type_goods_data=array();
   	    foreach ($type_data as $v) {
   	    	  $goods_data=K('GoodsTypeView')->get_data(array('bd_type_tid'=>$v['tid']));
   	      	// 重组数组，以菜类名为键名
   	        $type_goods_data[$goods_data[0]['tname']]=$goods_data;
   	    }
        // p($type_goods_data);die;
        $this->assign('type_goods_data',$type_goods_data);

        // 3,购物车
        $getGoods=Cart::getGoods();
        p($_SESSION);
        p($getGoods);

        //显示视图
        $this->display();
    }
    // 购物车添加商品
    public function ajax_add(){
    	  $gid=Q('post.gid',0,'intval');
        $car_data=current(K('Goods')->get_data(array('gid'=>$gid)));
        // $goods_car=current(K('car')->get_data(array('bd_goods_gid'=>$gid)));
        //Ajax
        $data=array(
           'id'=>$gid,
           'num'=>1,
           'name'=>$car_data['gname'],
           'price'=>$car_data['new_price'],
           'old_price'=>$car_data['old_price'],
        );
        Cart::add($data);
        $this->ajax($data,'JSON');
    }
    // 购物车显示商品
    public function ajax_all(){
        $data=Cart::getAllData();
        $this->ajax($data,'JSON');
    }
    // 购物车消减商品
    public function ajax_dele(){
        // 减数量
        $gid=Q('post.gid',0,'intval');
        $car_data=current(K('Goods')->get_data(array('gid'=>$gid)));
        $data=array(
            'id'=>$gid,
            'num'=>-1,
            'name'=>$car_data['gname'],
            'price'=>$car_data['new_price'],
            'old_price'=>$car_data['old_price'],
        );
        Cart::add($data);
        // 减商品
        $datas=$_SESSION;
        $key='';
        foreach ($datas['cart']['goods'] as $k => $v) {
            if($v['num']==0){
                $key=$k;
                unset($_SESSION['cart']['goods'][$k]);
            }
        }
        $new_data=array(
            'id'=>$gid,
            'name'=>$car_data['gname'],
            'price'=>$car_data['new_price'],
            'old_price'=>$car_data['old_price'],
            'k'=>$key
        );
        $this->ajax($new_data,'JSON');
    }
    public function ajax_empty(){
          unset($_SESSION['cart']);
          $this->ajax(1,'JSON');
    }
}
